//
//  XCHotListController.m
//  TTLestening
//
//  Created by dllo on 16/3/26.
//  Copyright © 2016年 dilo. All rights reserved.
//

#import "XCHotListController.h"
#import "NetworkingHandlerOfBlock.h"
#import "XCModelForSectionSinger.h"
#import "XCAVMusicPlayer.h"
#import "XCModelForSingerHeader.h"
#import "XCSingerHeaderView.h"
#import "XCCellForSIngerSection.h"

#define WIDGHTT [UIScreen mainScreen].bounds.size.width / 375
#define HEIGHTT [UIScreen mainScreen].bounds.size.height / 667

#define WIDGHT  self.view.frame.size.width
#define HEIGHT  self.view.frame.size.height


@interface XCHotListController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) NSMutableArray *arrayForHot;
@property (nonatomic, strong) XCModelForSingerHeader *modelForHeader;



@end

@implementation XCHotListController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self createTableView];
    //NSLog(@"%@", self.value);
    [self handleData];
    
    
}

-(void)handleData {

    NSString *urlFont = @"http://api.songlist.ttpod.com/songlists/";
    NSString *urlBack = @"?app=ttpod&v=v8.1.5.2016022418&user_id=0&mid=iPhone5C&f=f320&s=s310&imsi=&hid=&splus=9.2&active=1&net=2&openudid=71ae591d23e3955faffe1f13002174549b820ffe&idfa=766C509A-D4C4-4D11-89EA-A316DCF2B7CC&utdid=Vo%2FO9IPtv1UDAHSbJQCXwF%2F6&alf=201200&bundle_id=com.ttpod.music&latitude=38.88243&longtitude=121.5397";
    
 //   http://api.dongting.com/song/album/2336499?f=0&os=4.2.2&alf=10001791&imei=A0000038A97F12&from=android&resolution=720x1280&net=2&api_version=1.0&agent=none&v=v8.2.0.2015091720&utdid=VrYp2wJJ554DAL8NJZwP4bNJ&address=%E8%BE%BD%E5%AE%81%E7%9C%81%E5%A4%A7%E8%BF%9E%E5%B8%82%E6%B2%99%E6%B2%B3%E5%8F%A3%E5%8C%BA%E4%BA%94%E4%B8%80%E8%B7%AF314%E5%8F%B7&userId=0&longitude=121.54474&user_id=0&latitude=38.88339&language=zh
    
  //  NSLog(@"%@", self.value);
    
    
    NSString *url = [urlFont stringByAppendingFormat:@"%@%@", self.value, urlBack];
    
    [NetworkingHandler networkingWithUrl:url completeHandle:^(NSURLResponse *response, NSData *data, id result) {
        
       self.modelForHeader = [[XCModelForSingerHeader alloc] init];
        
        [self.modelForHeader setValuesForKeysWithDictionary:result];
        
          XCSingerHeaderView *view = [[XCSingerHeaderView alloc] initWithFrame:CGRectMake(0, 0, WIDGHT, HEIGHT / 3) passModelForHot:self.modelForHeader];
        
        self.tableview.tableHeaderView = view;
        
        self.arrayForHot = [NSMutableArray array];
        NSArray *arrayForSong = [result objectForKey:@"songs"];
        
        for (NSDictionary *dic in arrayForSong) {
            
            
            XCModelForSectionSinger *model = [[XCModelForSectionSinger alloc] init];
            
            [model setValuesForKeysWithDictionary:dic];
          
            
            [self.arrayForHot addObject:model];
            
        }
        
        
        [self.tableview reloadData];
    }];
    


}


- (void)createTableView {
    
    self.tableview.delegate = self;
    self.tableview.dataSource = self;
    [self.tableview registerClass:[XCCellForSIngerSection class] forHeaderFooterViewReuseIdentifier:@"headerFoot"];
    
#warning  注册功能的 cell

    [self.tableview registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    self.view.backgroundColor = [UIColor redColor];
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return self.arrayForHot.count;

}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {

    XCCellForSIngerSection *cell = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"headerFoot"];
    XCModelForSectionSinger *model = [self.arrayForHot objectAtIndex:section];
    [cell.button addTarget:self action:@selector(hidenAction:) forControlEvents:UIControlEventTouchUpInside];
    [cell.backButton addTarget:self action:@selector(playAction:) forControlEvents:UIControlEventTouchUpInside];
    cell.backButton.tag = section;
    cell.button.tag = section;

    [cell passModel:model sectionNumber:section + 1];
    
    return cell;


}
/**区头的点击事件 */
- (void)playAction:(UIButton *) button {
    
    XCAVMusicPlayer *music = [XCAVMusicPlayer sharedMusicPlayer];
    
   // XCModelForSectionSinger *model = [self.arrayForHot objectAtIndex:button.tag];
    
   //    gbnbnnmm,.
   //  [music passMusicUrl:[[model.urlList firstObject] objectForKey:@"url"]];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"giveValue" object:nil userInfo:@{@"modelArray": self.arrayForHot, @"index":[NSString stringWithFormat:@"%ld", button.tag]}];
    
    [music passMusicUrl:self.arrayForHot index:button.tag];
    
    
}

/** 隐藏分区下相应的 cell 事件*/
- (void)hidenAction:(UIButton *) button {
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"apperCell" object:nil userInfo:@{@"index": [NSString stringWithFormat:@"%ld",(long)button.tag]}];
    
    
    
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return 1;

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];

    return cell;
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
